<?php
namespace app\index\controller;
use think\worker\Server;
define('HEARTBEAT_TIME', 5);

class Worker extends Server{
    protected $socket = 'websocket://192.168.142.130:2345';
    public function index(){
        
    }
    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {   $connection->lastMessageTime = time();
        $connection->send('我收到你的信息了'. json_encode($data)  ."id=".$connection->id);
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
         global $text_worker, $global_uid;
        // 为这个连接分配一个uid
        $connection->uid = ++$global_uid;
        $connection->send('连接成功');
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        echo "启动了一个进程\n";
    }

}
